﻿namespace QFlow
{
    using System;
    using System.Windows.Forms;
    using DevComponents.DotNetBar;
    using QFlow.Core;
    using QFlow.Data;
    using QFlow.Windows.Forms;
    using QFlow.Windows;
    using QFlow.Windows.Reports;
    using QFlow.Reports;

    /// <summary>
    /// Main MDI form that holds the entier application
    /// </summary>
    public partial class MainForm : DevComponents.DotNetBar.Office2007RibbonForm
    {
        //the main DB context of the application
        private QFlow.Data.QFlowDbContext _Context;

        //Accounting log form
        private QFlow.Windows.Forms.AccountingLog _AccoutingLog;



        /// <summary>
        /// Initializes a new instance of the <see cref="MainForm"/> class.
        /// </summary>
        public MainForm(QFlow.Data.QFlowDbContext context)
        {
            InitializeComponent();
            _Context = context;
            _AccoutingLog = new QFlow.Windows.Forms.AccountingLog(_Context);
            _AccoutingLog.MdiParent = this;
        }

        /// <summary>
        /// Shows the chiled form.
        /// </summary>
        /// <param name="frm">The form to be shown.</param>
        private void ShowChildForm(Form frm)
        {
            frm.MdiParent = this;
            frm.Show();
        }

        #region Inventory

        /// <summary>
        /// Handles the Click event of the btnSearchProducts control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnSearchProducts_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new ProductsList(ProductType.Stockable, _Context));
        }

        /// <summary>
        /// Handles the Click event of the btnNewProduct control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnNewProduct_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new NewProduct(ProductType.Stockable, false, _Context));
        }

        /// <summary>
        /// Handles the Click event of the btnAdjustment control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnAdjustment_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new ProductAdjustment(_Context));
        }

        /// <summary>
        /// Handles the Click event of the btnProductStatus control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnProductStatus_Click(object sender, EventArgs e)
        {
            ShowChildForm(new ProductStatus(_Context));
        }

        /// <summary>
        /// Handles the Click event of the btnSearchNonStockable control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnSearchNonStockable_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new ProductsList(ProductType.NonStockable, _Context));
        }

        /// <summary>
        /// Handles the Click event of the btnNewNonStockable control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnNewNonStockable_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new NewProduct(ProductType.NonStockable, false, _Context));
        }

        /// <summary>
        /// Handles the Click event of the btnServiceSearch control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnServiceSearch_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new ProductsList(ProductType.Service, _Context));
        }

        /// <summary>
        /// Handles the Click event of the btnNewService control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnNewService_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new NewProduct(ProductType.Service, false, _Context));
        }

        /// <summary>
        /// Handles the Click event of the btnBrands control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnBrands_Click(object sender, EventArgs e)
        {
            //special case form
            //it must be sent a new context to fix binding sourse bugs
            this.ShowChildForm(new BrandsList(new QFlowDbContext()));
        }

        /// <summary>
        /// Handles the Click event of the btnCategories control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnCategories_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new CategoriesList(_Context));
        }

        /// <summary>
        /// Handles the Click event of the btnUnits control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnUnits_Click(object sender, EventArgs e)
        {
            //special case form
            //it must be sent a new context to fix binding sourse bugs
            this.ShowChildForm(new UnitsList(new QFlowDbContext()));
        }

        #endregion

        #region PurchaseOrders

        /// <summary>
        /// Handles the Click event of the btnSearchVendors control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnSearchVendors_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new VendorsList(_Context));
        }

        /// <summary>
        /// Handles the Click event of the btnNewVendor control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnNewVendor_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new NewVendor(false, _Context));
        }

        /// <summary>
        /// Handles the Click event of the btnPoSearch control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnPoSearch_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new PurchaseOrdersList(_Context));
        }

        /// <summary>
        /// Handles the Click event of the btnPoNew control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnPoNew_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new NewPurchaseOrder(false, _Context));
        }
        #endregion

        #region SoldOrders

        /// <summary>
        /// Handles the Click event of the btnNewCustomer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnNewCustomer_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new NewCustomer(false, _Context));
        }

        /// <summary>
        /// Handles the Click event of the btnSearchCustomers control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnSearchCustomers_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new CustomersList(_Context));
        }

        /// <summary>
        /// Handles the Click event of the btnSOSearch control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnSOSearch_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new SalesOrdersList(_Context));
        }

        /// <summary>
        /// Handles the Click event of the btnSONew control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnSONew_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new NewSalesOrder(false, _Context));
        }

        #endregion

        #region Reports

        private void btnInventory_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new InventoryReportViewer(_Context));
        }

        private void btnInventoryDetails_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new InventoryDetailsReportViewer(_Context));
        }

        private void btnProfitReport_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new ProfitReportViewer(_Context));
        }

        #endregion

        #region Administartion

        /// <summary>
        /// Handles the Click event of the btnUserSearch control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnUserSearch_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new UsersList(_Context));
        }

        /// <summary>
        /// Handles the Click event of the btnNewUser control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnNewUser_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new NewUser(false, _Context));
        }

        /// <summary>
        /// Handles the Click event of the btnRoleSearch control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnRoleSearch_Click(object sender, EventArgs e)
        {
            //special case form
            //it must be sent a new context to fix binding sourse bugs
            this.ShowChildForm(new RolesList(new QFlowDbContext()));
        }

        /// <summary>
        /// Handles the Click event of the btnNewRole control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnNewRole_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new NewRole(false, _Context));
        }

        /// <summary>
        /// Handles the Click event of the btnAccountingLog control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnAccountingLog_Click(object sender, EventArgs e)
        {
            if (_AccoutingLog == null || _AccoutingLog.IsDisposed)
            {
                _AccoutingLog = new QFlow.Windows.Forms.AccountingLog(_Context);
                _AccoutingLog.MdiParent = this;
            }
            _AccoutingLog.Show();
            _AccoutingLog.Activate();
        }

        /// <summary>
        /// Handles the Click event of the btnAccountingHistory control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnAccountingHistory_Click(object sender, EventArgs e)
        {
            this.ShowChildForm(new AccountingHistory(_Context));
        }

        #endregion

        /// <summary>
        /// Handles the FormClosing event of the MainForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            LoggingManager.LogoutUser(Authentication.CurrentUser as User);
        }

        private void btnAccountingLogs_Click(object sender, EventArgs e)
        {
            ShowChildForm(new AccountingLogsReportViewer(_Context));
        }

        private void btnReturns_Click(object sender, EventArgs e)
        {
            ShowChildForm(new ReturnsReportViewer(_Context));
        }
    }
}
